let { claimsModel } = require('./models/claimsModel')
module.exports.getClaims = async function ({ pageSize, currentPage, searchType = 'applyTime', searchValue = '' }) {
    //读取总条数
    let total = await claimsModel.countDocuments({
        isDel: false, [searchType]: {
            $regex: searchValue, $options: '$i'
        }
    });
    //总页数
    let pages = Math.ceil(total / pageSize)
    let claims = await claimsModel.find({
        isDel: false, [searchType]: {
            $regex: searchValue, $options: '$i'
        }
    }).populate({
            path: 'ordersId',
            populate: {
            path: 'insuresId usersId personalId'
        }
        })
        .limit(pageSize - 0) // 设置请求条数
        .skip((currentPage - 1) * pageSize) //设置跳过条数
    return {
        total, pages, claims
    }
}

module.exports.addClaims = async ({ applyTime, handleTime, accidentTime, images }) => {
    return await claimsModel.create({ applyTime, handleTime, accidentTime, images })
}

module.exports.deleteClaimsById = async (deleteId) => {
    return await claimsModel.updateOne(deleteId, { isDel: true })
}

module.exports.getClaimsById = async _id => {
    return await claimsModel.findById(_id)
}

module.exports.updateClaims = async ({ applyTime, handleTime, accidentTime, images, _id }) => {
    return await claimsModel.updateOne({ _id }, { applyTime, handleTime, accidentTime, images })
}

module.exports.againReapply = async (againId) => {
    return await claimsModel.updateOne(againId, { state: 1 })
}

module.exports.agreeReapply = async (againId) => {
    return await claimsModel.updateOne(againId, { state: 2 })
}

module.exports.disagreeReapply = async (againId) => {
    return await claimsModel.updateOne(againId, { state: 3 })
}